package com.xunqi.ad.service.impl;

import com.xunqi.ad.entity.AdPlan;
import com.xunqi.ad.entity.AdUser;
import com.xunqi.ad.enums.CommonStatusEnum;
import com.xunqi.ad.enums.ResponseEnum;
import com.xunqi.ad.exception.AdException;
import com.xunqi.ad.form.AdPlanForm;
import com.xunqi.ad.form.AdPlanGetForm;
import com.xunqi.ad.repository.AdPlanRepository;
import com.xunqi.ad.repository.AdUserRepository;
import com.xunqi.ad.service.IAdPlanService;
import com.xunqi.ad.vo.AdPlanResponseVo;
import com.xunqi.ad.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Description:
 * @Created with IntelliJ IDEA.
 * @author: 夏沫止水
 * @create: 2020-04-29 16:25
 **/
@Service("adPlanService")
public class AdPlanServiceImpl implements IAdPlanService {

    @Resource
    private AdPlanRepository adPlanRepository;

    @Resource
    private AdUserRepository adUserRepository;


    /**
     * 创建推广计划
     * @param form
     * @return
     * @throws AdException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseVo<AdPlanResponseVo> createAdPlan(AdPlanForm form) throws AdException {

        //确保关联的对象 User 对象是存在的
        Optional<AdUser> adUser = adUserRepository.findById(form.getUserId());
        //如果 user 不存在
        if (!adUser.isPresent()) {
            return ResponseVo.error(ResponseEnum.CAN_NOT_FIND_RECORD);
        }

        //查找是否有同名的planName
        AdPlan oldPlan = adPlanRepository.findByUserIdAndPlanName(form.getUserId(), form.getPlanName());
        if (oldPlan != null) {
            return ResponseVo.error(ResponseEnum.SAME_NAME_PLAN_ERROR);
        }

        //保存
        AdPlan newAdPlan = new AdPlan(form.getUserId(), form.getPlanName(), new Date(),new Date());
        AdPlan planSave = adPlanRepository.save(newAdPlan);

        AdPlanResponseVo adPlanResponseVo = new AdPlanResponseVo();
        BeanUtils.copyProperties(planSave, adPlanResponseVo);

        return ResponseVo.success(adPlanResponseVo);
    }

    /**
     * 获取推广计划
     * @param form
     * @return
     * @throws AdException
     */
    @Override
    public ResponseVo<List<AdPlan>> getAdPlanByIds(AdPlanGetForm form) throws AdException {

        List<AdPlan> adPlanList = adPlanRepository.findAllByIdInAndUserId(form.getIds(), form.getUserId());

        return ResponseVo.success(adPlanList);
    }

    /**
     * 更新推广计划
     * @param form
     * @return
     * @throws AdException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseVo<AdPlanResponseVo> updateAdPlan(AdPlanForm form) throws AdException {

        AdPlan plan = adPlanRepository.findByIdAndUserId(form.getId(), form.getUserId());

        if (plan == null) {
            return ResponseVo.error(ResponseEnum.CAN_NOT_FIND_RECORD);
        }

        if (form.getPlanName() != null) {
            plan.setPlanName(form.getPlanName());
        }
        if (form.getStartDate() != null) {
            plan.setCreateTime(new Date());
        }
        if (form.getEndDate() != null) {
            plan.setEndDate(new Date());
        }

        plan.setUpdateTime(new Date());
        AdPlan updatePlan = adPlanRepository.save(plan);

        AdPlanResponseVo adPlanResponseVo = new AdPlanResponseVo();
        BeanUtils.copyProperties(updatePlan, adPlanResponseVo);

        return ResponseVo.success(adPlanResponseVo);
    }


    /**
     * 删除推广计划
     * @param form
     * @return
     * @throws AdException
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseVo deleteAdPlan(AdPlanForm form) throws AdException {

        AdPlan plan = adPlanRepository.findByIdAndUserId(form.getId(), form.getUserId());
        if (plan == null) {
            return ResponseVo.error(ResponseEnum.CAN_NOT_FIND_RECORD);
        }
        //修改状态
        plan.setPlanStatus(CommonStatusEnum.INVALID.getStatus());
        plan.setUpdateTime(new Date());

        //进行保存
        adPlanRepository.save(plan);

        return ResponseVo.success();
    }
}
